package com.smart.home.netty.handler;

import com.smart.home.util.ResultCodeResolver;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;

/**
 * 有线输入发送信息、接收信息处理
 *
 * @author zhangpeng
 * @version 1.0
 * @date 2017-03-09
 */
public class WireInputCommandHandler extends AbstractChannelInboundHandler {

    public WireInputCommandHandler(String command) {
        super.setCommand(command);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        //接收返回数据
        String result = ByteBufUtil.hexDump(msg); //获取16进制数据
        //返回状态信息
        setStatus(ResultCodeResolver.wireInputResultStatus(result));
        //关闭信息通道
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //TODO 异常处理
        ctx.close();
    }
}
